//
//  LPKNetWorkTools.m
//  Demo-01
//
//  Created by 李平宽 on 2017/1/21.
//  Copyright © 2017年 com.lpk. All rights reserved.
//

#import "LPKNetWorkTools.h"

@implementation LPKNetWorkTools

+ (instancetype)sharedNetWorkTools
{
    static LPKNetWorkTools *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [LPKNetWorkTools manager];
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain",@"application/json", nil];
        
    });
    return instance;
}

- (void)request:(LPKNetWorkToolsMethod )method urlSrting:(NSString *)urlString parameters:(id)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    if (method == GET)
    {
        [self GET:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            success(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failure(error);
        }];
    }else{
        [self POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            success(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failure(error);
        }];
    }
}

-(void)HomeViewData:(NSString *)date success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    
    // ak = date=1%2F11&key=f776af77523b94205095dd8f40c0f17f
    
    NSString *url = @"http://api.juheapi.com/japi/toh";
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *datedd = [[NSDateFormatter alloc] init];
    [datedd setDateFormat:@"MM/dd"];
    
    NSString * dateddstring = [datedd stringFromDate:currentDate];
    NSArray *dateArr = [dateddstring componentsSeparatedByString:@"/"];
    
    NSDictionary *dict = @{@"month": dateArr[0] ,
                           @"day":dateArr[1],
                           @"key": @"f776af77523b94205095dd8f40c0f17f"
                           };
    [self request:GET urlSrting:url parameters:dict success:success failure:failure];
    
}
- (void)homeViewDetailDataId:(NSString *)Id success:(void(^)(id responseObject))success failure:(void(^)(NSError *error))failure
{
//    请求地址：http://api.juheapi.com/japi/tohdet
//    请求参数：v=&id=12760121&key=f776af77523b94205095dd8f40c0f17f
//    请求方式：GET
    // ak = f776af77523b94205095dd8f40c0f17f
    NSString *url = @"http://api.juheapi.com/japi/tohdet";

    NSDictionary *dict = @{@"id":Id,
                           @"key":@"f776af77523b94205095dd8f40c0f17f"};
    
    [self request:GET urlSrting:url parameters:dict success:success failure:failure];
    
}
@end
